package com.lzm.back.controller;

import com.github.pagehelper.PageInfo;
import com.lzm.back.common.MyResult;
import com.lzm.back.entity.File;
import com.lzm.back.service.FileService;
import jakarta.annotation.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private FileService fileService;

    //分页查询
    @GetMapping("/selectPage")
    public MyResult selectPage(@RequestParam(required = false) String eventName,@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<File> filePageInfo =fileService.selectPage(eventName, pageNum, pageSize);
        return MyResult.success(filePageInfo);
    }
    //查询单个
    @GetMapping("/{id}")
    public MyResult selectFileById(@PathVariable Long id) {
        File file = fileService.selectFileById(id);
        return MyResult.success(file);
    }
    //上传文件
    @PostMapping("/upload")
    public MyResult upload(@RequestParam Long eventId,@RequestParam(required = false) String fileName,@RequestParam(required = false) String filePath, MultipartFile file) throws IOException {
        fileService.upload(eventId, fileName, filePath, file);
        return MyResult.success();
    }
    //文件下载接口
    @GetMapping("/download/{id}")
    public ResponseEntity<org.springframework.core.io.Resource> downloadFile(@PathVariable Long id) throws IOException {
        org.springframework.core.io.Resource file = fileService.downloadFile(id);
        System.out.println(
                "file.getFilename() = " + file.getFilename()
        );
        String filename = file.getFilename();
        try {
            // 对文件名进行URL编码
            filename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(file);
    }
    @DeleteMapping("/{id}")
    public MyResult deleteFile(@PathVariable Long id) {
        fileService.deleteFile(id);
        return MyResult.success();
    }
    @DeleteMapping("/batch")
    public MyResult deleteFileBaTch(@RequestBody List<Long> ids) {
        fileService.deleteFileBaTch(ids);
        return MyResult.success();
    }
    //修改
    @PutMapping
    public MyResult updateFile(@RequestBody File file) {
        fileService.updateFile(file);
        return MyResult.success(file);
    }
}
